import { _decorator, Button, Component, EventHandler, Label, Node, ProgressBar, Sprite, SpriteFrame, sys } from 'cc';
import { EVENT_TYPE, MISSION_TYPE, REWARD_TYPE } from '../../Cube/Levels';
import DatasSpace from '../Tool/DatasSpace';
import EventMgr from '../Tool/EventMgr';
import { showTips } from '../Tips/Tips';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
const { ccclass, property } = _decorator;


export class Reward_Item {
  static target: Node;

  static customEventData: string;
}

@ccclass('dailyMissionItem')
export class dailyMissionItem extends Component {

  @property(Sprite) targetIcon: Sprite = null;
  @property(SpriteFrame) targetIconSpfs: SpriteFrame[] = [];

  @property(Sprite) targetTitle: Sprite = null;
  @property(SpriteFrame) targetTitleSpfs: SpriteFrame[] = [];

  @property(Label) targetLabel: Label = null;
  @property(ProgressBar) targetProgress: ProgressBar = null;

  @property(Node) rewardItem: Node = null;
  @property(SpriteFrame) rewardSpfs: SpriteFrame[] = [];

  @property(SpriteFrame) rewNum_x_1: SpriteFrame = null;
  @property(SpriteFrame) rewNum_x_20: SpriteFrame = null;
  @property(SpriteFrame) rewNum_x_25: SpriteFrame = null;
  @property(SpriteFrame) rewNum_x_30: SpriteFrame = null;
  @property(SpriteFrame) rewNum_x_40: SpriteFrame = null;
  @property(SpriteFrame) rewNum_x_50: SpriteFrame = null;
  @property(SpriteFrame) rewNum_x_100: SpriteFrame = null;
  @property(SpriteFrame) rewNum_x_200: SpriteFrame = null;

  public itemType: Daily_Mission = new Daily_Mission();

  private getMask: Node = null;
  start() {
  }

  private onEvent() {
  }

  private offEvent() {

  }
  protected onDestroy(): void {
    this.offEvent
  }

  protected onEnable(): void {
    this.initLocalData();
    this.initTargetLbl();
  }

  update(deltaTime: number) {

  }

  initMission(id, config: any) {
    this.itemType.id = id;
    this.itemType.missionType = config.type;
    this.itemType.targetNum = config.targetNum;
    this.itemType.rewardList = config.rewardList;
    this.itemType.isComplete = false;
    if (!DatasSpace.Instance.isSameDay) {
      this.itemType.isGet = false;
      DatasSpace.Instance.setDailyItem(this.itemType.id, false);
    } else {
      this.itemType.isGet = DatasSpace.Instance.getDailyConfig(this.itemType.id);
    }

    this.getMask = this.node.getChildByName('GetMask');
    this.getMask.active = this.itemType.isGet;

    this.targetIcon.spriteFrame = this.targetIconSpfs[config.type] ? this.targetIconSpfs[config.type] : this.targetIconSpfs[this.targetIconSpfs.length - 1];
    this.targetTitle.spriteFrame = this.targetTitleSpfs[config.type]

    this.rewardItem.children[0].getComponent(Sprite).spriteFrame = this.rewardSpfs[config.rewardList[0][0]];
    this.rewardItem.children[1].getComponent(Sprite).spriteFrame = this[`rewNum_x_${config.rewardList[0][1]}`];

    this.initLocalData();
    this.initTargetLbl();

    //console.log(this.itemType)
  }

  private initLocalData() {
    this.itemType.dataNum = DatasSpace.Instance.getInt('DailyMission_' + this.itemType.missionType, 0);
    if (this.itemType.dataNum >= this.itemType.targetNum) {
      this.itemType.dataNum = this.itemType.targetNum;
      this.itemType.isComplete = true;
    }
  }

  private initTargetLbl() {
    switch (this.itemType.missionType) {
      case MISSION_TYPE.LEVEL:
        this.targetLabel.string = `完成${this.itemType.targetNum}个麻将关卡`
        break;
      case MISSION_TYPE.SHARE:
        this.targetLabel.string = `分享游戏${this.itemType.targetNum}次`
        break;
      case MISSION_TYPE.STARBOX_GET_COUNT:
        this.targetLabel.string = `领取星星宝箱${this.itemType.targetNum}次`
        break;
      case MISSION_TYPE.LEVELBOX_GET_COUNT:
        this.targetLabel.string = `领取关卡宝箱${this.itemType.targetNum}次`
        break;
      case MISSION_TYPE.USE_PROP_CHEHUI:
        this.targetLabel.string = `使用${this.itemType.targetNum}个撤回道具`
        break;
      case MISSION_TYPE.USE_PROP_FANPAI:
        this.targetLabel.string = `使用${this.itemType.targetNum}个翻牌道具`
        break;
      case MISSION_TYPE.USE_PROP_SHIZHONG:
        this.targetLabel.string = `使用${this.itemType.targetNum}个加时`
        break;
      case MISSION_TYPE.USE_PROP_XIAOCHU:
        this.targetLabel.string = `使用${this.itemType.targetNum}个消除道具`
        break;
      case MISSION_TYPE.VIDEO_COUNT:
        this.targetLabel.string = `累计观看视频${this.itemType.targetNum}次`
        break;
      case MISSION_TYPE.SIGN:
        this.targetLabel.string = `登录奖励`
        break;

      default:
        break;
    }

    this.targetProgress.node.getComponentInChildren(Label).string = `${this.itemType.dataNum}/${this.itemType.targetNum}`;
    this.targetProgress.progress = this.itemType.dataNum / this.itemType.targetNum;
  }


  btnReward() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    // Reward_Item.target = this.node;
    // Reward_Item.customEventData = this.itemType.id + '';
    // this.node.emit('btnReward', Reward_Item);
    //console.log(this.itemType.isComplete)
    if (this.itemType.isComplete) {
      console.log(this.itemType.rewardList)
      let Rew_Type = this.itemType.rewardList[0][0];
      let Rew_Num = this.itemType.rewardList[0][1];
      switch (Rew_Type) {
        case REWARD_TYPE.CHEHUI:
          DatasSpace.Instance.propNumArr[3] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.FANPAI:
          DatasSpace.Instance.propNumArr[1] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.XIAOCHU:
          DatasSpace.Instance.propNumArr[2] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.XIPAI:
          DatasSpace.Instance.propNumArr[0] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.SHIZHONG:
          DatasSpace.Instance.propNumArr[4] += Rew_Num;
          DatasSpace.Instance.setPropNum();
          break;
        case REWARD_TYPE.COIN:
          DatasSpace.Instance.goldNum += Rew_Num;
          DatasSpace.Instance.setGoldNum();
          break;
        default:
          break;
      }
      showTips('领取成功');
      this.itemType.isGet = true;
      this.getMask.active = this.itemType.isGet;
      DatasSpace.Instance.setDailyItem(this.itemType.id, this.itemType.isGet);

      EventMgr.emit(EVENT_TYPE.EVENT_UPD_DAILYMISSIONLIST);
    }
  }
}

export class Daily_Mission {
  id: number = 0;
  missionType: MISSION_TYPE = null
  targetNum: number = 0;
  dataNum: number = 0;
  isComplete: boolean = false;
  isGet: boolean = false;
  rewardList: number[][] = [];
}
